﻿/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Globalization;

namespace MyPhotoIndex.Plugins.WebHost
{
    class Utils
    {
        internal static string GetContentTypeFromFileExtention(string fileExtention)
        {
            string contentType = "text/html";

            if (string.Compare(fileExtention, ".xml") == 0)
            {
                contentType = "text/xml";
            }
            else
                if (string.Compare(fileExtention, ".gif") == 0)
                {
                    contentType = "image/gif";
                }
                else
                    if (string.Compare(fileExtention, ".jpg") == 0)
                    {
                        contentType = "image/jpg";
                    }
                    else
                        if (string.Compare(fileExtention, ".png") == 0)
                        {
                            contentType = "image/png";
                        }
                    else
                        if (string.Compare(fileExtention, ".html") == 0)
                        {
                            contentType = "text/html";
                        }
            return contentType;
        }

        internal static void SetCacheHeadersOneYear(HttpListenerResponse response, DateTime lastModified)
        {
            response.AddHeader("Cache-Control", "public, max-age=31536000");
            response.AddHeader("Date", DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US")));
            response.AddHeader("Expires", DateTime.Now.Add(new TimeSpan(365, 0, 0, 0, 0)).ToString(CultureInfo.GetCultureInfo("en-US")));
            response.AddHeader("Last-Modified", lastModified.ToString(CultureInfo.GetCultureInfo("en-US")));
        }

        internal static void SetCacheHeadersOneHour(HttpListenerResponse response, DateTime lastModified)
        {
            response.AddHeader("Cache-Control", "public, max-age=31536000");
            response.AddHeader("Date", DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US")));
            response.AddHeader("Expires", DateTime.Now.Add(new TimeSpan(0, 0, 60, 0, 0)).ToString(CultureInfo.GetCultureInfo("en-US")));
            response.AddHeader("Last-Modified", lastModified.ToString(CultureInfo.GetCultureInfo("en-US")));
        }

        internal static void CopyStreams(Stream fs, Stream output)
        {
            byte[] buffer = new byte[4096];
            int bytesRead = 0;

            do
            {
                bytesRead = fs.Read(buffer, 0, 4096);
                if (bytesRead == 0)
                {
                    break;
                }

                output.Write(buffer, 0, bytesRead);

            } while (bytesRead == 4096);
        }


        internal static void EndResponse(HttpListenerRequest request, HttpListenerResponse response, int statusCode)
        {
            response.StatusCode = statusCode;
            //response.StatusDescription = "invalid image file type\r\nSupported types: gif, jpg, zip, rss";
            response.Close();
        }

        internal static string GetRequestFileName(HttpListenerRequest request)
        {
            return VirtualPathUtility.GetFileName(request.Url.AbsolutePath);
        }

        internal static string GetRequestExtension(HttpListenerRequest request)
        {
            return VirtualPathUtility.GetExtension(request.Url.AbsolutePath);
        }
    }
}
